package GUI;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import Monitoring.Settings;
import Structs.Key;

public class SettingsInterface extends JFrame{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4815450471381450223L;
	private GridBagLayout layout = new GridBagLayout();
	private JTextArea fileDirectoryLocation;
	private JTextArea mainSceneKeyArea;
	private JTextArea characterSelectionKeyArea;
	private JTextArea ingameKeyArea;
	private Settings settings;
	
	private Key csKey;
	private Key defaultScreenKey;
	private Key gameKey;
	
	public SettingsInterface(Settings settings){
		this.settings = settings;
		
		csKey = settings.charSelectionKey;
		defaultScreenKey = settings.defaultSceneKey;
		gameKey = settings.ingameKey;
		
		this.setLayout(layout);
		setup();
		this.pack();
		this.setVisible(true);
	}
	
	public void setup(){
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.ipadx = 10;
		JLabel fileDirectory = new JLabel("Directory");
		JLabel mainScene = new JLabel("Main Scene");
		JLabel characterSelection = new JLabel("Champ Select");	
		JLabel ingame = new JLabel("Ingame");	

		this.add(fileDirectory,c);
		c.gridy = 1;
		this.add(mainScene,c);
		c.gridy = 2;
		this.add(characterSelection,c);
		c.gridy = 3;
		this.add(ingame,c);	
		
		 fileDirectoryLocation = new JTextArea(settings.fileDirectory);
		
		characterSelectionKeyArea = new JTextArea(settings.charSelectionKey.getKey());	
		mainSceneKeyArea = new JTextArea(settings.defaultSceneKey.getKey());
		ingameKeyArea = new JTextArea(settings.ingameKey.getKey());

		c.gridx = 1;
		c.gridy = 0;
		c.weightx = 0.2;

		this.add(fileDirectoryLocation,c);
		c.gridy = 1;
		this.add(mainSceneKeyArea,c);
		c.gridy = 2;
		this.add(characterSelectionKeyArea,c);
		c.gridy = 3;
		this.add(ingameKeyArea,c);		
		
		JButton fileDirectoryBrowse = new JButton("Browse");
		fileDirectoryBrowse.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				final JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = fc.showOpenDialog(fc);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					fileDirectoryLocation.setText(fc.getSelectedFile().getPath());
				}
		    };
		   
					
		});
		JButton mainSceneNewKey = new JButton("New key");
		mainSceneNewKey.addActionListener(new ActionListener(){
			Input i;
			@Override
			public void actionPerformed(ActionEvent arg0) {
				i = new Input();
				i.addKeyListener(new KeyListener(){

					@Override
					public void keyPressed(KeyEvent ke) {
						defaultScreenKey = new Key(KeyEvent.getKeyText(ke.getKeyCode()),ke.getKeyCode());
						mainSceneKeyArea.setText(defaultScreenKey.getKey());
						i.setVisible(false);
						i.dispose();
					}

					@Override
					public void keyReleased(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void keyTyped(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
				});
			}
			
			
		});
		
		
		JButton characterSelectionNewKey= new JButton("New key");
		characterSelectionNewKey.addActionListener(new ActionListener(){
			Input i;
			@Override
			public void actionPerformed(ActionEvent arg0) {
				i = new Input();
				i.addKeyListener(new KeyListener(){

					@Override
					public void keyPressed(KeyEvent ke) {
						csKey = new Key(KeyEvent.getKeyText(ke.getKeyCode()),ke.getKeyCode());
						characterSelectionKeyArea.setText(csKey.getKey());
						i.setVisible(false);
						i.dispose();
					}

					@Override
					public void keyReleased(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void keyTyped(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
				});
			}
			
			
		});
		
		JButton ingameKeyButton = new JButton("New key");
		ingameKeyButton.addActionListener(new ActionListener(){
			Input i;
			@Override
			public void actionPerformed(ActionEvent arg0) {
				i = new Input();
				i.addKeyListener(new KeyListener(){

					@Override
					public void keyPressed(KeyEvent ke) {
						gameKey = new Key(KeyEvent.getKeyText(ke.getKeyCode()),ke.getKeyCode());
						ingameKeyArea.setText(gameKey.getKey());
						i.setVisible(false);
						i.dispose();
					}

					@Override
					public void keyReleased(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void keyTyped(KeyEvent arg0) {
						// TODO Auto-generated method stub
						
					}
					
				});
			}
			
			
		});
		c.gridx = 2;
		c.gridy = 0;
		
		this.add(fileDirectoryBrowse,c);
		c.gridy = 1;		
		this.add(mainSceneNewKey,c);
		c.gridy = 2;
		this.add(characterSelectionNewKey,c);
		c.gridy = 3;		
		this.add(ingameKeyButton,c);
		
		JButton saveButton = new JButton("Save");
		saveButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				settings.setFileDirectory(fileDirectoryLocation.getText());
				settings.setCharSelectionKey(csKey);
				settings.setDefaultSceneKey(defaultScreenKey);
				settings.setIngameKey(gameKey);
				saveToFile();
				setVisible(false);
				dispose();
			}
			
			
		});
		c.gridx = 0;
		
		c.gridy = 4;
		c.fill = GridBagConstraints.NONE;
		this.add(saveButton, c);
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				dispose();
			}
			
			
		});
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;
		this.add(cancelButton, c);
	}
	
	
	public void saveToFile(){
		PrintWriter writer;
		try {
			writer = new PrintWriter("OBSLolSceneSwapper.ini");
			writer.println("Path: " + settings.getFileDirectory());
			writer.println("Default Scene: " + settings.defaultSceneKey.getKey() +" " + settings.defaultSceneKey.getKeyCode());
			writer.println("Champ Select Scene: " + settings.getCharSelectionKey().getKey() +" " + settings.getCharSelectionKey().getKeyCode());
			writer.println("Ingame Scene: " + settings.getIngameKey().getKey() +" " + settings.getIngameKey().getKeyCode());
			writer.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
}
